%macro get_w2;

	
	proc printto log = "&logdir/get_w2.txt" new;
	run;
	

		/****************************
		*	Get the tins 			*
		*	into 'tinlist'			*
		****************************/
		%make_tinlist;

		/****************************************
		*										*
		*			Start PROC SQL				*
		*										*
		****************************************/
		proc sql noprint;
		[connection details redacted]
			create table sample as
			select * from connection to iq 
			(
			select
				a.person_id,
				b.[redacted] as payer_id,
				b.[redacted] as wgs,
				b.[redacted] as formdate,
				b.[redacted] as med_wh,
				b.[redacted] as wh,
				coalesce(b.[redacted],0) +
					coalesce(b.[redacted],0) + 
					coalesce(b.[redacted],0) + 
					coalesce(b.[redacted],0) +
					coalesce(b.[redacted],0) as dc_contrib,
				b.[redacted] as tips,
				b.[redacted] as med_wgs
			from #tinlist as a
				inner join [redacted] as b
					on(a.person_id = b.[redacted])
			where b.[redacted] = &base_yr
				and b.[redacted] > 0
				and b.[redacted] <= 4
			order by
				a.person_id,
				b.[redacted],
				formdate
			);
			disconnect from iq;
			quit;

		/****************************************
		*			Drop duplicates				*
		*			(keep most recent			*
		*			within payer_tin, tin)		*
		****************************************/
		data sample;
			set sample;
			by person_id payer_id;
			if last.payer_id;
			drop formdate;
		run;

		/********************************
		*								*
		*		Sum by individual		*
		*								*
		********************************/
		proc means data = sample nway noprint;
			class person_id;
			output out = rlib.w2(drop=_type_ _freq_)
				sum(wgs wh med_wh med_wgs tips dc_contrib) 
					= wgs w2_wh med_wh med_wgs tips dc_contrib;
		run;



	proc printto;
	run;
	
	
%mend;
